package com.hisan.freeride.home.activity;


import android.os.Bundle;
import android.view.View;

import com.github.library.BaseQuickAdapter;
import com.github.library.listener.OnItemClickListener;
import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.base.BaseAppLication;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.view.MyGridLayoutManager;
import com.hisan.freeride.databinding.LovecartBinding;
import com.hisan.freeride.home.adapter.Love_CartAdapter;
import com.hisan.freeride.home.model.LoveCartModel;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建时间 : 2017/6/12
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：爱心车主页面
 */
public class LoveCartActivity extends BaseActivity<LovecartBinding> {
    private Love_CartAdapter adapter;
    private int page=1;
    private List<LoveCartModel> dataBeen=new ArrayList<>();
    @Override
    protected int getLayoutId() {
        return R.layout.lovecart;
    }

    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        adapter=new Love_CartAdapter(new ArrayList<>());
        mBinding.rv.setAdapter(adapter);
        mBinding.rv.setLayoutManager(new MyGridLayoutManager(this,2));
    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
        mBinding.rv.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
                LoveCartModel loveCartModel= (LoveCartModel) baseQuickAdapter.getItem(i);
                if (!CollectionUtils.isNullOrEmpty(loveCartModel)){
                    int id=loveCartModel.getUser_id();
                    Bundle bundle=new Bundle();
                    bundle.putInt("driver_id",id);
                    startActivityForResult(LoveCartDetailsActivity.class,0,bundle,true);
                }
            }
        });
        mBinding.pull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                page=1;
                dataBeen.clear();
                loadData(true);
                mBinding.pull.finishRefresh();

            }

            @Override
            public void loadMore() {
                page++;
                loadData(true);
                mBinding.pull.finishLoadMore();
            }
        });
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<List<LoveCartModel>>get(AppConfig.Driverlist)
                .tag(this)
                .params("pageNumber",page)
                .params("pageSize",20)
                .params("city_id", BaseAppLication.getInstance().getCityid())
                .execute(new DialogCallback<List<LoveCartModel>>(this) {
                    @Override
                    public void onSuccess(Response<List<LoveCartModel>> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            if(response.body().size()>=20){
                                mBinding.pull.setCanLoadMore(true);
                            }else {
                                mBinding.pull.setCanLoadMore(false);
                            }
                            dataBeen=response.body();
                            showData();
                            adapter.setNewData(dataBeen);
                        }else {
                            if (dataBeen.size()>0){
                                adapter.setNewData(dataBeen);
                            }else {
                                noData();
                            }
                        }
                    }

                    @Override
                    public void onError(Response<List<LoveCartModel>> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }
}
